package com.ross.web.dao;

import java.util.LinkedList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;

import com.ross.web.domain.Product;
import com.ross.web.domain.ReturnProduct;
import com.ross.web.util.HibernateUtil;

public class ReturnProductDAO extends HibernateUtil {
	
public List getAllReturnProducts(){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			
			result = session.createCriteria(ReturnProduct.class, "rp")
				.setProjection(Projections.projectionList()
						.add(Projections.property("rp.rp_id"),"rp_id")
						.add(Projections.property("rp.productids"),"productids")
						.add(Projections.property("rp.returndate"),"returndate")
						.add(Projections.property("rp.returntime"),"returntime")
						.add(Projections.property("rp.remarks"),"remarks")
						.add(Projections.property("rp.totalvalue"),"totalvalue")
						)
						.setResultTransformer(new AliasToBeanResultTransformer(ReturnProduct.class))
						.list();
			
			
			
		return result;
			
		}finally {
			session.close();
		}		
	}
	
	public ReturnProduct getReturnProductById(int rp_id){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{	
			result = session.createCriteria(ReturnProduct.class, "rp")
				.setProjection(Projections.projectionList()
						.add(Projections.property("rp.rp_id"),"rp_id")
						.add(Projections.property("rp.productids"),"productids")
						.add(Projections.property("rp.returndate"),"returndate")
						.add(Projections.property("rp.returntime"),"returntime")
						.add(Projections.property("rp.remarks"),"remarks")
						.add(Projections.property("rp.totalvalue"),"totalvalue")
						)
						.add(Restrictions.eq("rp.rp_id", rp_id))
						.setResultTransformer(new AliasToBeanResultTransformer(ReturnProduct.class))
						.list();
			
			
			
		return result.isEmpty() ? null : (ReturnProduct) result.get(0);
			
		}finally {
			session.close();
		}		
	}
	
	public void insert(ReturnProduct rp){
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.save(rp);
			tx.commit();
		}catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		
	}
	
	public void update(ReturnProduct rp){
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.update(rp);
			tx.commit();
		}catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		
	}
	
	public void delete(ReturnProduct rp){
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.delete(rp);
			tx.commit();
		}catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		
	}

}
